package com.onpositive.wiki3.db.impl;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashSet;

import com.onpositive.wiki3.db.catmodel.Categories;
import com.onpositive.wiki3.db.catmodel.Category;
import com.onpositive.wiki3.db.catmodel.ICategoryMatcher;
import com.onpositive.wiki3.db.catmodel.matches.LeafsRemover;
import com.onpositive.wiki3.db.catmodel.matches.SelfLinksRemover;

public class Test {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		final WikiDBImpl wikiDBImpl = new WikiDBImpl("C:/work/content");
		try {
			Categories c = new Categories(wikiDBImpl, new File(
					"C:/work/content/n2.cats"), false);
			HashSet<Category> ms = new HashSet<Category>();
			HashSet<String> ms1 = new HashSet<String>();
			// for (Category cc:c){
			// String lowerCase = cc.toString().toLowerCase();
			// if (lowerCase.contains("tanks")){
			// if (lowerCase.contains("finland")){
			// System.out.println("------");
			// System.out.println(lowerCase);
			// System.out.println("------");
			// for (Category m:cc.getChildCategories()){
			// System.out.println(m);
			// }
			// System.out.println("****");
			// for (Category m:cc.getParentsCategories()){
			// System.out.println(m);
			// }
			// }
			// }
			// if (lowerCase.equals("ships")){
			// System.out.println(cc.id());
			// ms1.add(lowerCase);
			// ms.add(cc);
			// // System.out.println("------");
			// // System.out.println(lowerCase);
			// // System.out.println("------");
			// // for (Category m:cc.getChildCategories()){
			// // System.out.println(m);
			// // }
			// // System.out.println("****");
			// // for (Category m:cc.getParentsCategories()){
			// // System.out.println(m);
			// // }
			// }
			// // if (lowerCase.equals("watercraft")){
			// // System.out.println("------");
			// // System.out.println(lowerCase);
			// // System.out.println("------");
			// // for (Category m:cc.getChildCategories()){
			// // System.out.println(m);
			// // }
			// // System.out.println("****");
			// // for (Category m:cc.getParentsCategories()){
			// // System.out.println(m);
			// // }
			// // }
			// }
			// System.out.println(ms1);
			// System.out.println(ms);
			// int id =
			// wikiDBImpl.getTitleMapper().getId("Disambiguation_pages");
			Category st = c.get("ships");
			Category[] parents = st.getParentsCategories();
			System.out.println(Arrays.toString(parents));
			System.out.println("A");
			// c.accept(new LeafsRemover());
			System.out.println(c.size());
			// Category category = c.get("tanks");
			// for (Category m:category.getChildCategories()){
			// System.out.println(m);
			// }
			// System.out.println("****");
			// for (Category m:category.getParentsCategories()){
			// System.out.println(m);
			// }

			// c.removeCategory(id);

			HashSet<Category> ss = new HashSet<Category>();
			HashSet<Category> roots = new HashSet<Category>();
			for (Category m : c) {
				if (m.hasChildren()) {
					ss.add(m);
				}
			}
			for (Category m : c) {
				if (m.parentCount() == 0) {
					roots.add(m);
				}
			}

			// for (Category m:c.get("contents").getChildCategories()){
			// System.out.println(m);
			// }

			// category = c.get("tanks");
			// for (Category m:category.getChildCategories()){
			// System.out.println(m);
			// }
			c.get("hidden_categories").remove();
			// Wikipedia_administration
			c.get("categories").remove();
			c.get("wikiprojects").remove();
			// c.get("Featured Content").remove();
			c.get("help").remove();
			c.get("wikipedia_administration").remove();
			c.removeAllContaining(wikiDBImpl, "categories_by_");
			// /c.get("Wikipedia_categories").remove();
			// System.out.println(ss.size());
			// System.out.println(roots.size());
			// c.accept(new MultiLevelMatcher(1));

			System.out.println(c.size());
			Category category = c.get("ships");
			category.getRootPath();
			System.out.println(category.distance());

			category = c.get("tanks_by_country");
			System.out.println(category.distance());
			category = c.get("tanks");
			System.out.println(category.distance());
			category = c.get("aircraft");
			System.out.println(category.distance());
			// for (Category m : c.all()) {
			// if (m.distance() > 30) {
			// roots.add(m);
			// c.removeCategory(m.id());
			// }
			// }
			// c.removeAllContaining(wikiDBImpl, "loanwords");
			// c.removeAllContaining(wikiDBImpl, "_words");
			// c.removeAllContaining(wikiDBImpl, "academia");
			int l = 0;
			for (Category m : c) {
				if (m.distance() < 8) {
					l++;
				}
			}
			System.out.println("Cluster size:" + l);
//			c.accept(new ICategoryMatcher() {
//
//				@Override
//				public int accept(Category st) {
//					Category[] parentsCategories = st.getParentsCategories();
//					String title = st.toString();
//					if (title.length() > 4) {
//						HashSet<Category> s = null;
//						for (Category m : parentsCategories) {
//							String string = m.toString();
//							if (string.length() > 4) {
//								if (title.contains(string)) {
//									if (s == null) {
//										s = new HashSet<Category>();
//									}
//									s.add(m);
//								}
//							}
//						}
//						if (s != null) {
//							for (Category m : parentsCategories) {
//								if (!s.contains(m)) {
//									st.unregisterParent(m);
//								}
//							}
//						}
//					}
//					return 0;
//				}
//			});
			c.fillLevels("contents", wikiDBImpl);
			//c.accept(new BackLinksRemover(4));
			//c.accept(new SelfLinksRemover());
//			for (int a=0;a<10;a++){
//				c.accept(new LeafsRemover());
//			}
			System.out.println("Sizing:");
			int mc=0;
			for (Category m : c) {

				// System.out.println(m);
				int deepSize = m.deepSize();
				if (deepSize==-1){
					mc++;
				}
//				if (deepSize == -1) {
//					System.err
//							.println("Cycles found:" + m + ":" + m.distance());
//					// break;
//				}
				// /System.out.println(k);
			}
			Category[] all = c.all();
			Arrays.sort(all,new Comparator<Category>() {

				@Override
				public int compare(Category o1, Category o2) {
					return o2.deepSize()-o1.deepSize();
				}
			});;
			for (Category cq:all){
				if (cq.deepSize()>10000){
					System.out.println(cq+":"+cq.deepSize()+":"+cq.distance());
				}
			}
			System.out.println(mc);
			System.out.println(c.size());

			System.out.println("Removals:"
					+ SelfLinksRemover.getTotalRemovals());
			// for (int a = 0; a < 8; a++) {
			// c.accept(new LeafsRemover());
			// c.validateIntegrity();
			// c.accept(new RootsRemover());
			// }
			// //c.accept(new SelfLinksRemover());
			// for (int a = 0; a < 10; a++) {
			// c.accept(new LeafsRemover());
			// c.accept(new RootsRemover());
			// }
			System.out.println(c.size());
			c.write(new File("C:/work/content/n7.cats"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// createCats(wikiDBImpl);
		// int findPrimaryForm =
		// PrimaryArticleFinder.findPrimaryCategory("car",wikiDBImpl);
		// if (findPrimaryForm!=-1){
		// System.out.println(wikiDBImpl.getTitleMapper().getTitle(findPrimaryForm));
		// }
		//
		// int[] categoryMembers =
		// wikiDBImpl.getCategoryAccessor().getCategoryMembers(findPrimaryForm,
		// false);
		// for (int q:categoryMembers){
		// System.out.println(wikiDBImpl.getTitleMapper().getTitle(q));
		//
		// }
		// System.setProperty("wordnet.database.dir", "C:/work/wn/dict");
		// int size = wikiDBImpl.getTitleMapper().size();
		// int[] allCategoriesWithChilren = wikiDBImpl.getCategoryAccessor()
		// .getAllCategoriesWithChilren();
		// CountingVocabalary words = new CountingVocabalary();
		// CountingVocabalary composites = new CountingVocabalary();

		// WordNetDatabase database = WordNetDatabase.getFileInstance();
		// Synset[] synsets = database.getSynsets("fly", SynsetType.NOUN);
		//
		// ITemplateAccess temlates = wikiDBImpl.getTemlates();
		// for (int q : allCategoriesWithChilren) {
		// String title = wikiDBImpl.getTitleMapper().getTitle(q);
		// String lowerCase = title.toLowerCase();
		// String replace = lowerCase.replace('_', ' ');
		// int lastIndexOf = replace.lastIndexOf(" by ");
		// if (lastIndexOf != -1) {
		// String work = replace.substring(lastIndexOf + 4);
		// if (work.indexOf(' ') != -1) {
		// composites.add(work);
		// } else {
		// words.add(work);
		// }
		// }
		// }
		//
		// for (Pair p : words.all()) {
		// if (p.i > 20) {
		//
		// // database = WordNetDatabase.getFileInstance();
		// synsets = database.getSynsets(p.s, SynsetType.NOUN);
		// if (synsets.length > 0) {
		//
		// ITemplateAccess temlates2 = wikiDBImpl.getTemlates();
		// int majorTemplateCount = temlates2.getMajorTemplateCount();
		// for (int a = 0; a < majorTemplateCount; a++) {
		// String s = temlates2.getTemplateName(a);
		// if (!s.startsWith("infobox")) {
		// continue;
		// }
		// if (s.endsWith(p.s)){
		// System.out.println(s);
		// int i = temlates2.getPages(a)[0];
		// System.out.println(wikiDBImpl.getTitleMapper().getTitle(i));
		// }
		// }
		// }
		// // for (int i = 0; i < synsets.length; i++) {
		// // NounSynset nounSynset = (NounSynset) (synsets[i]);
		// // NounSynset[] hyponyms = nounSynset.getHyponyms();
		// // System.err.println(nounSynset.getWordForms()[0] + ": "
		// // + nounSynset.getDefinition() + ") has "
		// // + hyponyms.length + " hyponyms");
		// // }
		// }
		// }
	}

	protected static void createCats(WikiDBImpl wikiDBImpl) {
		CategoryAccessImpl categoryAccessor = (CategoryAccessImpl) wikiDBImpl
				.getCategoryAccessor();
		int id = wikiDBImpl.getTitleMapper().getId("Anarchism");

		int size = wikiDBImpl.getTitleMapper().size();
		for (int a = 0; a < size; a++) {
			String title = wikiDBImpl.getTitleMapper().getTitle(a);
			if (title.equals("Momtazuddin Ahmed (dramatist)")) {
				// Momtazuddin Ahmed (dramatist)
				int redirect = wikiDBImpl.getRedirectAccess().getRedirect(a);
				if (redirect != -1) {
					if (redirect == id) {

						System.out.println(title);
					}
					// buildInfoMap2.replaceAll(a,redirect);
				}
			}
		}
		Categories buildInfoMap2 = categoryAccessor.buildInfoMap2();
		System.out.println(buildInfoMap2.size());

		buildInfoMap2.validateIntegrity();
		buildInfoMap2.removeAllContaining(wikiDBImpl, "wikipedia");
		buildInfoMap2.removeAllContaining(wikiDBImpl,
				"_with_dead_external_links");
		buildInfoMap2.removeAllContaining(wikiDBImpl,
				"_with_unsourced_statements");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "template:");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "talk:");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "file:");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "wikiproject");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "user:");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "outline");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "Articles_containing_");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "_redirects");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "_external_links");
		buildInfoMap2.removeAllContaining(wikiDBImpl, "_language_text");
		// _needing_additional_references_
		buildInfoMap2.removeAllContaining(wikiDBImpl,
				"_including_recorded_pronunciations_");
		buildInfoMap2.removeAllContaining(wikiDBImpl,
				"_needing_additional_references_");
		id = wikiDBImpl.getTitleMapper().getId("Hidden_categories");
		buildInfoMap2.removeWithChildren(id);
		buildInfoMap2.validateIntegrity();
		// System.out.println(buildInfoMap2.size());

		// int size = wikiDBImpl.getTitleMapper().size();
		for (int a = 0; a < size; a++) {
			int redirect = wikiDBImpl.getRedirectAccess().getRedirect(a);
			if (redirect != -1) {

				buildInfoMap2.replaceAll(a, redirect);
			}
		}
		System.out.println(buildInfoMap2.size());
		// buildInfoMap2.validateIntegrity();
		buildInfoMap2.normalizeSmaller();
		// /doPrint(wikiDBImpl, buildInfoMap2);
		try {
			buildInfoMap2.write(new File("C:/work/content/n.cats"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	protected static void doPrint(WikiDBImpl wikiDBImpl,
			Categories buildInfoMap2) {
		for (Category c : buildInfoMap2) {
			int[] parents = c.getParents();
			if (parents.length > 1) {
				String ctitle = wikiDBImpl.getTitleMapper().getTitle(c.id());
				System.out.println(ctitle);
				System.out.println("*****");
				for (int q : parents) {
					String title = wikiDBImpl.getTitleMapper().getTitle(q);
					System.out.println(title);
				}
			} else {
				String ctitle = wikiDBImpl.getTitleMapper().getTitle(c.id());
				if (parents.length == 1) {
					String title = wikiDBImpl.getTitleMapper().getTitle(
							parents[0]);
					System.out.println(title);
				} else {
					System.out.println(ctitle);
				}
			}
		}
	}

}
